package lunchy.mvc.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;

import lunchy.data.interfaces.ILunchyMenuGroup;
import lunchy.main.CLunchyApplication;
import lunchy.mvc.controllers.CLunchyMenuEditGroupController;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;

public class CLunchyMenuEditGroupView
{
	private Shell shellMenuEditAddGroup;
	private CLunchyMenuEditGroupController controller;
	private Text groupName;

	public CLunchyMenuEditGroupView(ILunchyMenuGroup group)
	{
		controller = new CLunchyMenuEditGroupController(group);
		
		shellMenuEditAddGroup = new Shell(CLunchyApplication.getInstance().display, SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.APPLICATION_MODAL);
		shellMenuEditAddGroup.setMinimumSize(new Point(200, 80));
		shellMenuEditAddGroup.setText("LunchY | Menu Edit");
		shellMenuEditAddGroup.setSize(272, 87);

		initUI();
		
		groupName.setText(group.getName());
		
		shellMenuEditAddGroup.open();
        
        while (!shellMenuEditAddGroup.isDisposed())
        {
        	if (!CLunchyApplication.getInstance().display.readAndDispatch())
        		CLunchyApplication.getInstance().display.sleep();
        }
	}

	private void initUI()
	{
		groupName = new Text(shellMenuEditAddGroup, SWT.BORDER);
		groupName.setBounds(75, 7, 187, 19);
		
		Label nameLabel = new Label(shellMenuEditAddGroup, SWT.NONE);
		nameLabel.setBounds(10, 10, 59, 14);
		nameLabel.setText("Название");
		
		Button addButton = new Button(shellMenuEditAddGroup, SWT.NONE);
		addButton.setBounds(75, 32, 94, 28);
		addButton.setText("Сохранить");
		addButton.addMouseListener(
			new MouseListener()
			{
				public void mouseUp(MouseEvent e)
				{
					controller.modifyGroup(groupName.getText());
					shellMenuEditAddGroup.dispose();
				}

				public void mouseDown(MouseEvent e)
				{
	
				}

				public void mouseDoubleClick(MouseEvent e)
				{

				}
			}
		);
		
		Button cancelButton = new Button(shellMenuEditAddGroup, SWT.NONE);
		cancelButton.setBounds(168, 32, 94, 28);
		cancelButton.setText("Отмена");
		cancelButton.addMouseListener(
			new MouseListener()
			{
				public void mouseUp(MouseEvent e)
				{
					shellMenuEditAddGroup.dispose();
				}

				public void mouseDown(MouseEvent e)
				{

				}

				public void mouseDoubleClick(MouseEvent e)
				{

				}
			}
		);
	}
}
